package sv.com.renacempleo.dao.red;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.renacempleo.dao.BaseDaoImpl;
import sv.com.renacempleo.dto.red.RedDemandantes;
import sv.com.renacempleo.exception.WebException;
import sv.com.renacempleo.web.util.WebUtilidades;

public class RedDemandanteDaoImpl extends BaseDaoImpl implements RedDemandanteDao{

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActivesAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(RedDemandantes.class);
		criteria.addOrder(Order.asc("redDemandanteNombres"));
		criteria.addOrder(Order.asc("redDemandanteApellidos"));
		criteria.add(Restrictions.eq("redDemandanteActivo", "0"));
		criteria.createAlias("wkfEstadoSeguimiento", "wkfEstadoSeguimiento", Criteria.LEFT_JOIN);
		criteria.createAlias("sgdUsuario", "sgdUsuario");
		criteria.createAlias("sgdUsuario.ctgOficina", "ctgOficina");
		criteria.createAlias("ctgMunicipioResidencia", "ctgMunicipio", Criteria.LEFT_JOIN);
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("redDemandanteFolio")).
				add(Projections.property("redDemandanteFullName")).
				add(Projections.property("redDemandanteFechaRegistro")).
				add(Projections.property("wkfEstadoSeguimiento.ctgCatalogoNombre")).
				add(Projections.property("sgdUsuario.sgdUsuarioUsuario")).
				add(Projections.property("redDemandanteDUI")).
				add(Projections.property("ctgOficina.ctgOficinaNombre")).
				add(Projections.property("redDemandanteActivo")).
				add(Projections.property("redDemandanteTelefono1"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllInactivesAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(RedDemandantes.class);
		criteria.addOrder(Order.asc("redDemandanteNombres"));
		criteria.addOrder(Order.asc("redDemandanteApellidos"));
		criteria.add(Restrictions.eq("redDemandanteActivo", "0"));
		return (List<Object[]>) findByCriteria(criteria);
	}

	@Override
	public List<Object[]> findAllAsArray() throws WebException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Object[]> findDemandanteByOficina() throws WebException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Object[]> findDemandanteByAgente() throws WebException {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findDemandanteByDocumento(Long ctgDocumentoId, String redDemandanteNumeroDocumento) throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(RedDemandantes.class);
		criteria.add(Restrictions.eq("ctgDocumento.ctgDocumentoId",ctgDocumentoId));
		criteria.add(Restrictions.eq("redDemandanteNumeroDocumento",redDemandanteNumeroDocumento));
		return (List<Object[]>) findByCriteria(criteria);
	}
	
	@SuppressWarnings({ "unchecked" })
	public RedDemandantes getDemandante(Long redDemandanteFolio) throws WebException{
		DetachedCriteria criteria = DetachedCriteria.forClass(RedDemandantes.class);
		criteria.add(Restrictions.eq("redDemandanteFolio",redDemandanteFolio));
		List<RedDemandantes> list = (List<RedDemandantes>) findByCriteria(criteria);
		if(!WebUtilidades.isEmptyList(list)) return list.get(0);
		return null;
	}

	

}
